//
//  LZFTabBar.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/21.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFTabBar.h"
#import "LZFPublishVC.h"



@interface LZFTabBar ()
@property(nonatomic,strong)UIButton *plusButton;
@property(nonatomic,strong)UIControl *previousClickedButton;
@end

@implementation LZFTabBar

-(UIButton *)plusButton
{
    if (_plusButton==nil) {
        _plusButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [_plusButton addTarget:self action:@selector(plusButtonDidClicked) forControlEvents:UIControlEventTouchUpInside];
        [_plusButton sizeToFit];
        [self addSubview:_plusButton];
    }
    
    return _plusButton;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x=0;
    CGFloat y=0;
    CGFloat w=self.bounds.size.width/5;
    CGFloat h=self.bounds.size.height;
    int i=0;
    //这里要给遍历出来的tabBarButton添加target触发,可是uiview并不能addTarget,那么可以
    //借助 UIControl是UIView的子类, 子类可以自动转换成父类的特性,使用UIControl来接受遍历出来的button
    
    //UIView *tabBarButton in self.subviews
    for (UIControl *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            
            if (i==0&self.previousClickedButton==nil) {
                self.previousClickedButton=tabBarButton;
            }
            if (i==2) {
                i=i+1;
            }
            x=i*w;
            tabBarButton.frame=CGRectMake(x, 0, w, h);
            i++;
            
            [tabBarButton addTarget:self action:@selector(tabBarButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    
    
  //  self.plusButton.frame=CGRectMake(self.bounds.size.width*0.5,self.bounds.size.height*0.5, w, h);
  self.plusButton.center=CGPointMake(self.frame.size.width*0.5,self.frame.size.height*0.5);
 
}

//tabbarbutton按钮被点击了
-(void)tabBarButtonDidClicked:(UIButton *)currentButton
{

    if (self.previousClickedButton==currentButton) {
        //重复点击tabBarButton,发送通知
        [[NSNotificationCenter defaultCenter]postNotificationName:LZFTabBarButtonDidRepeatClickedNotification object:nil];
    }
}


//plusButton被点击了
-(void)plusButtonDidClicked
{
     LZFPublishVC *publishVC=[[LZFPublishVC alloc]init];
    [self.window.rootViewController presentModalViewController:publishVC animated:NO];
    
}


@end
